package com.haoshuang.zhouzhoubang.ui.page.order.assignment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityTaskAssignmentBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderDetailBean
import com.haoshuang.zhouzhoubang.model.bean.RenWuBean
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.ktx.toJson
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load

/**
 *  任务指派
 */
class TaskAssignmentActivity :
    BaseVMActivity<TaskAssignmentViewModel, ActivityTaskAssignmentBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<TaskAssignmentActivity>()
        }
    }
    var principalsList = arrayListOf<RenWuBean.TeamMemberData>()
    var memberList = arrayListOf<RenWuBean.TeamMemberData>()
    private val mPrincipalsAdapter = MemberListAdapter(principalsList, 1)
    private val mMemberAdapter = MemberListAdapter11(memberList, 1)
    var list6 = arrayListOf<OrderDetailBean.TeamMember>()

    var user_id9 = ""

    // 是否返回数据
    private var returnResult = false

    // 是否是返工订单
    private var is_rework = false

    var ids = ""
    var team_member = ""

    // 是不是个人订单 默认不是
    private var isPersonal = false

    override fun initView(savedInstanceState: Bundle?) {
        isPersonal = intent.getBooleanExtra("isPersonal", false)

        returnResult = intent.getBooleanExtra("returnResult", false)
        is_rework = intent.getBooleanExtra("is_rework", false)
        val layoutManager = LinearLayoutManager(this)    // 创建布局管理者
        val layoutManager1 = LinearLayoutManager(this)    // 创建布局管理者

        binding.titleBar.title = "任务指派"
        binding.titleBar.leftImageView().click { finish() }

        binding.apply {
            rvPrincipals.layoutManager = layoutManager    // 给recycleview设置布局管理者
            rvPrincipals.adapter = mPrincipalsAdapter    // 给recycleview设置适配器对象

            rvMember.layoutManager = layoutManager1    // 给recycleview设置布局管理者
            rvMember.adapter = mMemberAdapter    // 给recycleview设置适配器对象
            mMemberAdapter.setOnItemChildClick(id = R.id.viewClick) { view, position, item ->
                val bean=memberList[position]
                if (isPersonal){
                    for (b in memberList) {
                        b.isCHeck = 0
                    }
                    memberList[position].isCHeck = 1
                    mMemberAdapter.notifyDataSetChanged()
                }else{
                    if (bean.isCHeck==0){
                        memberList[position].isCHeck=1
                    }else{
                        memberList[position].isCHeck=0
                    }
                    mMemberAdapter.notifyDataSetChanged()
                }

            }
            mPrincipalsAdapter.setOnItemChildClick(id = R.id.viewClick){ view, position, item ->
                for (b in principalsList) {
                    b.isCHeck = 0
                }
                principalsList[position].isCHeck = 1
                user_id9 = item?.to_user_id.toString()
                mPrincipalsAdapter.notifyDataSetChanged()
            }

            tvCommit.clickWithTrigger {

                if (user_id9.isNullOrBlank()){
                    toast("请选择负责人")
                    return@clickWithTrigger
                }
                team_member=""
                for (b in memberList){
                    if (b.isCHeck==1){
                        team_member=  team_member+b.to_user_id+","
                    }
                }
                if (team_member.isNullOrBlank()){
                    toast("请选择成员")
                    return@clickWithTrigger
                }
                if (returnResult){
                    val member = principalsList.firstOrNull { it.isCHeck == 1 }
                    val members = memberList.filter { it.isCHeck == 1 }
                    setResult(200, Intent().apply {
                        putExtra("member" , member?.toJson())
                        putExtra("members" , members.toJson())
                    })
                    finish()
                }else {
                    vm.addAppoint(
                        ids.toString(),
                        user_id9,
                        team_member.substring(0, team_member.length - 1)
                    )
                }
            }
        }

    }

    override fun initData() {
        ids=intent.getString("ids")

        vm.getServiceOrderDetail(ids)
    }

    override fun startObserve() {
        vm.addAppointData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("指派成功")
                val member = principalsList.firstOrNull { it.isCHeck == 1 }
                val members = memberList.filter { it.isCHeck == 1 }
                setResult(200, Intent().apply {
                    putExtra("member" , member?.toJson())
                    putExtra("members" , members.toJson())
                })
                finish()
            }
        }

        vm.getServiceOrderDetailData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it != null) {

                    vm.appointData(ids)
                    binding.apply {

                        if (it.data?.team_member?.isNotEmpty() == true){

                            list6.clear()

                            list6.addAll(it?.data?.team_member ?: emptyList())

                        }
                        if (!it?.data?.team_leader?.user_id.isNullOrBlank()){

                            user_id9=it.data?.team_leader?.user_id ?:""

                        }


                    }
                }
            }
        }

        vm.appointDataData.observeState(this) {
            onLoading  { showLoading() }
            onError { _, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                val it = it?.data
                binding.apply {
                    ivHead.load(it?.team_data?.team_logo,R.mipmap.my_toux)
                    tvName.text=it?.team_data?.team_name
                }
                if (it!=null){
                    principalsList.clear()
                    memberList.clear()
                    if (it.team_member_data.size>0){
                        for (b in it.team_member_data){
                            if (!b.nickname.isNullOrBlank()){
                                if (user_id9==b.to_user_id.toString()){
                                    var bean=b
                                    bean.isCHeck=1
                                    principalsList.add(bean)
                                }else{
                                    principalsList.add(b)
                                }
                                val bean= RenWuBean.TeamMemberData(
                                    b.work_rest,b.add_time,b.avatar,b.captain,0,b.consent
                                    ,b.id,b.nickname,b.skill_name,b.team_id,b.to_user_id,b.who_apply,b.work_status)
                                memberList.add(bean)
                            }

                        }
                        mPrincipalsAdapter.notifyDataSetChanged()
                        for (b in list6){
                            for (b2 in memberList){
                                if (b.user_id.toInt()==b2.to_user_id){
                                    b2.isCHeck=1
                                }
                            }
                        }
                        mMemberAdapter.notifyDataSetChanged()
                    }else {
                        toast("没有可以指派人员")
                        finish()
                    }

                }

            }
        }
    }

}